{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "连接成功\n",
      "<xtquant.xttype.StockAccount object at 0x000001F5BF727A60>\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "from xtquant.xttrader import XtQuantTrader\n",
    "\n",
    "# miniQMT安装路径\n",
    "mini_qmt_path = r'D:\\国金证券QMT交易端\\userdata_mini'\n",
    "# 创建session_id\n",
    "session_id = int(random.randint(100000, 999999))\n",
    "# 创建交易对象\n",
    "xt_trader = XtQuantTrader(mini_qmt_path, session_id)\n",
    "# 启动交易对象\n",
    "xt_trader.start()\n",
    "# 连接客户端\n",
    "connect_result = xt_trader.connect()\n",
    "\n",
    "if connect_result == 0:\n",
    "    print('连接成功')\n",
    "\n",
    "from xtquant.xttype import StockAccount\n",
    "# QMT账号\n",
    "account_id = '1010573943'\n",
    "# 创建账号对象\n",
    "account = StockAccount(account_id)\n",
    "# 订阅账号\n",
    "res = xt_trader.subscribe(account)\n",
    "print(account)\n",
    "print(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "on order callback:\n",
      "600000.SH 54 53201\n",
      "on order callback:\n",
      "600000.SH 54 53196\n"
     ]
    }
   ],
   "source": [
    "from xtquant.xttrader import XtQuantTraderCallback\n",
    "\n",
    "class MyXtQuantTraderCallback(XtQuantTraderCallback):\n",
    "    def on_disconnected(self):\n",
    "        \"\"\"\n",
    "        连接状态回调\n",
    "        :return:\n",
    "        \"\"\"\n",
    "        print(\"connection lost\")\n",
    "    def on_stock_order(self, order):\n",
    "        \"\"\"\n",
    "        委托信息推送\n",
    "        :param order: XtOrder对象\n",
    "        :return:\n",
    "        \"\"\"\n",
    "        print(\"on order callback:\")\n",
    "        print(order.stock_code, order.order_status, order.order_sysid)\n",
    "    def on_stock_trade(self, trade):\n",
    "        \"\"\"\n",
    "        成交信息推送\n",
    "        :param trade: XtTrade对象\n",
    "        :return:\n",
    "        \"\"\"\n",
    "        print(\"on trade callback\")\n",
    "        print(trade.account_id, trade.stock_code, trade.order_id)\n",
    "\n",
    "# 注册回调类\n",
    "xt_trader.register_callback(MyXtQuantTraderCallback())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 同步报单"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1082425084\n"
     ]
    }
   ],
   "source": [
    "from xtquant import xtconstant\n",
    "# 下单\n",
    "order_id = xt_trader.order_stock(account, stock_code='600000.SH', order_type=xtconstant.STOCK_BUY, order_volume=100, price_type=xtconstant.LATEST_PRICE, price=-1)\n",
    "\n",
    "print(order_id)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 异步报单"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1080\n",
      "1081\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "on order callback:\n",
      "600000.SH 50 53368\n",
      "on order callback:\n",
      "600000.SH 50 53369\n"
     ]
    }
   ],
   "source": [
    "from xtquant import xtconstant\n",
    "# 下单\n",
    "seq1 = xt_trader.order_stock_async(account, stock_code='600000.SH', order_type=xtconstant.STOCK_BUY, order_volume=100, price_type=xtconstant.FIX_PRICE, price=7.00)\n",
    "\n",
    "seq2 = xt_trader.order_stock_async(account, stock_code='600000.SH', order_type=xtconstant.STOCK_BUY, order_volume=100, price_type=xtconstant.FIX_PRICE, price=7.00)\n",
    "\n",
    "print(seq1)\n",
    "print(seq2)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 同步撤单"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "on order callback:\n",
      "600000.SH 54 53368\n"
     ]
    }
   ],
   "source": [
    "from xtquant import xtconstant\n",
    "\n",
    "order_id = 1082269289\n",
    "\n",
    "res = xt_trader.cancel_order_stock(account, order_id)\n",
    "print(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1329\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "on order callback:\n",
      "600000.SH 54 53369\n"
     ]
    }
   ],
   "source": [
    "from xtquant import xtconstant\n",
    "\n",
    "order_id = 1082269288\n",
    "\n",
    "seq = xt_trader.cancel_order_stock_async(account, order_id)\n",
    "print(seq)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "miniqmt-demo-cptEsqYo-py3.10",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
